package kr.saeildamil.androidstudy_second28;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainXml extends Activity {
	ProgressDialog progressDlg;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		LinearLayout root = new LinearLayout(this);
		root.setOrientation(LinearLayout.VERTICAL);
		
		Button pullBtn = new Button(this);
		pullBtn.setText("Pull Parser");
		pullBtn.setOnClickListener(listener);
		pullBtn.setId(1);
		
		Button jsonBtn = new Button(this);
		jsonBtn.setText("JSON Parser");
		jsonBtn.setOnClickListener(listener);
		jsonBtn.setId(2);
		
		LayoutParams params = new LayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		root.addView(pullBtn, params);
		root.addView(jsonBtn, params);
		setContentView(root);
		
		new XmlJsonMake().execute();
	}
	
	private class XmlJsonMake extends AsyncTask<Void, Void, Void>{
		protected void onPreExecute() {
			super.onPreExecute();
			progressDlg = ProgressDialog.show(MainXml.this, "Wait", "order.xml & order.json Making...");
		}
		protected Void doInBackground(Void... params) {
			FileOutputStream fos = null;
			try {
				fos = openFileOutput("order.xml", MODE_PRIVATE);
				String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"  ?>\n"
							+"   <order>\n"
							+"       <item Maker=\"Samsung\" Price=\"23000\">Mouse</item>\n"
							+"       <item Maker=\"LG\" Price=\"12000\">Keyboard</item>\n"
							+"       <item Price=\"156000\" Maker=\"Western Digital\" >HDD</item>\n"
							+"   </order>";
				fos.write(xml.getBytes());
				fos = openFileOutput("order.json", MODE_PRIVATE);
				String json = "[\n"
							+ "  { \"Product\":\"Mouse\", \"Maker\":\"Samsung\", \"Price\":23000},\n"
							+ "  { \"Product\":\"Keyboard\", \"Maker\":\"LG\", \"Price\":12000},\n"
							+ "  { \"Product\":\"HDD\", \"Maker\":\"Western Digital\", \"Price\":156000}\n"
							+ "]";
				fos.write(json.getBytes());
				Thread.sleep(3000);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			} finally {
				try {fos.close();} catch(Exception e){;}
			}
			
			return null;
		}
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			progressDlg.dismiss();
			Toast.makeText(MainXml.this, "order.xml & order.json Make", Toast.LENGTH_SHORT).show();
		}
	}
	
	View.OnClickListener listener = new OnClickListener() {
		public void onClick(View v) {
			Intent intent = new Intent();
			switch(v.getId()){
			case 1: intent.setClass(MainXml.this, Pull_parserActivity.class); break;
			case 2: intent.setClass(MainXml.this, JSON_parserActivity.class); break;
			}
			startActivity(intent);
		}
	};
	
}
